package com.lm.liaoming.tool;

import android.content.Context;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**格式化时间戳
 * 韦敏敏
 */
public class DateUtil {

    public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
    private static SimpleDateFormat sf = null;

    public static final String DATE_FULL = "yyyy年MM月dd日";

    /**
     * 将字符串转为时间戳
     *
     * @param time
     */
    public static long getStringToDate(String time, String parseString) {
        sf = new SimpleDateFormat(parseString);
        Date date = null;
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }


    /*时间戳转换成字符窜-2*/
    public static String getDateToString2(long time) {
//        Date d = new Date(time);
        sf = new SimpleDateFormat(DATE_FULL_STR);
        return sf.format(time);
    }


    /**
     * 计算两个日期型的时间相差多少时间
     *
     * @param startDate 开始日期
     * @return
     */
    public static String twoDateDistance(long startDate) {

        Date endDate = Calendar.getInstance().getTime();
        if (startDate == 0 || endDate == null) {
            return null;
        }

        long timeLong = endDate.getTime() - startDate;

        if (isYeaterday(startDate, null) == 0) {
            Date sDate = new Date(startDate);
            int hour = sDate.getHours();
            int min = sDate.getMinutes();

            String sMin = String.valueOf(min);
            if (sMin.length() == 1) {
                sMin = "0" + sMin;
            }
            return "昨天 " + hour + ":" + sMin;
        }

        if (timeLong < 60 * 1000)
//            return timeLong / 1000 + "秒前";
            return "刚刚";

        else if (timeLong < 60 * 60 * 1000) {
            timeLong = timeLong / 1000 / 60;
            return timeLong + "分钟前";
        } else if (timeLong < 60 * 60 * 24 * 1000) {
            timeLong = timeLong / 60 / 60 / 1000;
            return timeLong + "小时前";
        } else if (timeLong < 60 * 60 * 24 * 1000 * 10) {
            timeLong = timeLong / 1000 / 60 / 60 / 24;
            return (timeLong + 1) + "天前";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            return sdf.format(startDate);
        }
    }

    /**
     * @param oldTime 较小的时间
     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * @throws ParseException 转换异常
     * @author LuoB.
     */
    public static int isYeaterday(long oldTime, Date newTime) {
        if (newTime == null) {
            newTime = new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = null;
        try {
            today = format.parse(todayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //昨天 86400000=24*60*60*1000 一天
        if ((today.getTime() - oldTime) > 0 && (today.getTime() - oldTime) <= 86400000) {
            return 0;
        } else if ((today.getTime() - oldTime) <= 0) { //至少是今天
            return -1;
        } else { //至少是前天
            return 1;
        }

    }

    /**
     * 判断两个日期是否是同一天
     *
     * @param date1 date1
     * @param date2 date2
     * @return
     */
    private static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }
    /**
     * 格式化时间
     * @param time
     * @return
     */
    public static String formatTime(String time) {
        Long i = Long.parseLong(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date curDate = new Date(i);//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }
    public static String formatTime2(String time) {
        Long i = Long.parseLong(time);
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd  HH:mm");
        Date curDate = new Date(i);//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }
    public static String formatTimeNYR(String time) {
        Long i = Long.parseLong(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(i);//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }
    /***
     * 获取系统当前时间
     * @return
     */
    public static String getCurrentTime(){
        long l = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(l);//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }
    public static String getCurrentTime(String datet){
        long l = Long.parseLong(datet);
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        Date curDate = new Date(l);//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    /**
     * 转化几小时前的样式
     * time:11位时间，需乘1000
     */
    public static String getTimeAgo(String time){
        long createTime = Long.parseLong(time)*1000;//创建时间
        long currentTime = System.currentTimeMillis();//当前时间
        long spacTime = (currentTime - createTime)/1000;
        String returnTime = "";

//        if (spacTime<60){
//            //间隔时间不足一分钟
//            holder.time.setText("刚刚");
//        }else if (spacTime<3600){
//            //时间间隔不足一小时
//            holder.time.setText(spacTime/60+"分钟前");
//        }else if (spacTime<86400){
//            //时间间隔不足一天
//            holder.time.setText(spacTime/60/60+"小时前");
//        }else if (spacTime<(60*60*24*30)){
//            //时间间隔不足一个月
//            holder.time.setText(spacTime/60/60/24+"天前");
//        }else if (spacTime<(60*60*24*30*12)){
//            //时间间隔不足一年
//            holder.time.setText(spacTime/60/60/24/30+"月前");
//        }else if (spacTime>=(60*60*24*30*12)){
//            //时间间隔超过一年
//            holder.time.setText(spacTime/60/60/24/30/12+"年前");
//        }

        if (spacTime<=86400){
            //时间不足一天,今天(显示几点几分)
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date date = new Date(createTime);
            returnTime = sdf.format(date);
        }else if (spacTime<86400*2&&spacTime>86400){
            //昨天，(显示昨天 几点几分)
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date date = new Date(createTime);
            returnTime = "昨天 "+sdf.format(date);
        }else if (spacTime<=0){
            returnTime = "未知";
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = new Date(createTime);
            returnTime = sdf.format(date);
        }

        return returnTime;
    }

    private static Context context;
    private static final long DAY = 24 * 60 * 60 * 1000;
    private static final long DAY_SCEONDS = 24 * 60 * 60;

    private static final int SECOND = 1000;
    private static final int MINUTE = SECOND * 60;
    private static final int HOUR = MINUTE * 60;
    private static final int MAX_LIVE_TIME = 24 * HOUR;

    /*字符串转时间戳*/
    public static Long stringDataToLong(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
    /*获取年份*/
    public static String getYear(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        return simpleDateFormat.format(Long.parseLong(data));
    }

    public static String getYear(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String time = simpleDateFormat.format(data);
        return time;
    }

    public static String getMonth(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        return simpleDateFormat.format(Long.parseLong(data));
    }
    /**获取月
     * */
    public static String getMonth(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        return simpleDateFormat.format(data);
    }
    /**获取月
     * */
    public static String getMonthDays(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        return simpleDateFormat.format(data);
    }
    /***
     *获取天
     */
    public static String getDays(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        return simpleDateFormat.format(Long.parseLong(data));
    }

    public static String getDays(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        String day = simpleDateFormat.format(data);
        return day;
    }

    public static String getHour(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        return simpleDateFormat.format(Long.parseLong(data));
    }

    public static String getHour(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        String day = simpleDateFormat.format(data);
        return day;
    }

    public static String getMinute(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm");
        return simpleDateFormat.format(Long.parseLong(data));
    }

    public static String getMinute(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm");
        String day = simpleDateFormat.format(data);
        return day;
    }
    public static String getyuetian(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String day = simpleDateFormat.format(data);
        return day;
    }
    public static String getfenmiao(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String day = simpleDateFormat.format(data);
        return day;
    }

    public static String getSecond(String data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss");
        return simpleDateFormat.format(Long.parseLong(data));
    }

    public static String getSecond(Long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss");
        String day = simpleDateFormat.format(data);
        return day;
    }

    public static String formatNewsData(Long time) {
        if (time == null || time == 0) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    public static String formatCommentData(Long time) {
        if (time == null || time == 0) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    public static String getCommentTimer(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sd = sdf.format(new Date(time));
        return sd;
    }
    public static String getSFM(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String sd = sdf.format(new Date(time));
        return sd;
    }
    public static String getTimer(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(time));
        return sd;
    }

    public static String formatReceiveTime(String times) {
        if (times == null || times.isEmpty()) {
            return "";
        }
        Long time = Long.parseLong(times);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String sd = sdf.format(new Date(time));
        return sd;
    }


    /**
     * @param times 这里传的时间是毫秒值的String串
     * @return
     */
    public static boolean isOutTime(String times) {
        if (times == null || times.isEmpty()) {
            return true;
        }
        Long time = Long.parseLong(times);
        Long timeNow = System.currentTimeMillis();
        return (timeNow - time) > DAY;
    }

    /**
     * @param time 这个time是秒值,不是毫秒值
     * @return
     */
    public static boolean isOutTime(long time) {
        if (time == 0L) {
            return true;
        }
        Long timeNow = System.currentTimeMillis() / 1000;
        return (timeNow - time) > DAY_SCEONDS;
    }


    /**
     * 将 字符串转换成时间戳
     *
     * @param inputDate
     * @return
     */
    public static long getLongTimeForString(String inputDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        try {
            date = sdf.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    //获取倒计时
    public static String getCountdown(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        return sdf.format(time);
    }

    public static String getMillisecond(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("ss");
        return sdf.format(time);
    }


    public static String getAuctionTimer(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(time));
        return sd;
    }

    //获取直播的总时长的字符串
    public static String formatLiveTime(Long l) {
        int hour = 0;
        int minute = 0;
        int second = 0;

        second = l.intValue() / 1000;

        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        return (getTwoLength(hour) + ":" + getTwoLength(minute) + ":" + getTwoLength(second));
    }

    private static String getTwoLength(final int data) {
        if (data < 10) {
            return "0" + data;
        } else {
            return "" + data;
        }
    }
    //计算当前所在周
    public static List<String> getTimeInterval(Date date) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        list.add(imptimeBegin);
        for (int i = 0; i < 6; i++) {
            cal.add(Calendar.DATE, 1);
            list.add(sdf.format(cal.getTime()));
        }
        return list;
    }

    /*把年月日转换成时间戳*/
    /*
     * 将时间转换为时间戳
     */
    public  long dateToStamp(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return ts;
    }
}
